package com.lab.utils.imagechooser;

import android.Manifest;
import android.content.Intent;

import com.cuitrip.service.R;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;
import com.lab.app.BaseFragment;
import com.lab.rtenvironment.AppCoreRuntime;
import com.lab.utils.ToastUtil;
import com.lab.widget.imageselector.MultiImageSelectorActivity;

/**
 * Created by ak on 4/21/16.
 */
public class EasyImageChooser {

    public static final int MAX_CHOOSE_SIZE = 20;

    public static boolean isInit = false;


    public static void chooseImage(final BaseFragment fragment, final int requestCode, final int nowSize) {
        if (nowSize >= MAX_CHOOSE_SIZE) {
            ToastUtil.toast(AppCoreRuntime.context.getString(R.string.operation_upload_photo_limit_1_more, MAX_CHOOSE_SIZE));
            return;
        }
        if (Dexter.isRequestOngoing()) {
            return;
        }
        Dexter.checkPermission(new PermissionListener() {

            @Override
            public void onPermissionGranted(PermissionGrantedResponse response) {
                int selectedMode = MultiImageSelectorActivity.MODE_MULTI;

                Intent intent = new Intent(fragment.getContext(), MultiImageSelectorActivity.class);
                // 最大可选择图片数量
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, MAX_CHOOSE_SIZE - nowSize);
                // 选择模式
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, selectedMode);

                fragment.startActivityForResult(intent, requestCode);
            }

            @Override
            public void onPermissionDenied(PermissionDeniedResponse response) {

            }

            @Override
            public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
                if (null != token) {
                    token.continuePermissionRequest();
                }
            }


        }, Manifest.permission.READ_EXTERNAL_STORAGE);
    }



}
